﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicServer;
using Model;
using CommonCs;

namespace BugSysSolution
{
    public partial class FileUpLoad : System.Web.UI.Page
    {
        private static string guid = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind(1);
            }
        }

        public void Bind(int index)
        {
            UpLoadFileLogic uf = new UpLoadFileLogic();
            //this.Pager.AlwaysShow = true;
            //this.Pager.RecordCount = uf.GetFileCount(this.Pager.PageSize, index);
            //this.rep1.DataSource = uf.GetAllFile(this.Pager.PageSize, index);
            this.rep1.DataSource = uf.GetAllFileNoPageing();
            this.rep1.DataBind();
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            Bind(this.Pager.CurrentPageIndex);
        }

        protected void btn_files_Click(object sender, EventArgs e)
        {
            bool bl = UpFile();
            UpLoadFileLogic uf = new UpLoadFileLogic();
            Users user = Session["user"] as Users;
            FilesTable ft = new FilesTable();
            ft.CreateDate = DateTime.Now;
            ft.CreateUser = user.Id;
            ft.FileName = this.up_file.FileName;
            ft.FileUrl = "UpLoadFiles/" + guid + "-" + this.up_file.FileName;
            bool bl1 = uf.AddFile(ft);
            if (bl)
            {
                if (bl1)
                {
                    Common.WriteFile(CommonCs.Exceptions.Operational, user.Uid, "文件[" + this.up_file.FileName + "]添加数据库记录成功!");
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "reload", "window.location.href = window.location.href", true);
                }
                Common.WriteFile(CommonCs.Exceptions.Operational, user.Uid, "文件[" + this.up_file.FileName + "]添加数据库记录失败!");
            }
        }

        public bool UpFile()
        {
            Users user = Session["user"] as Users;
            guid = System.Guid.NewGuid().ToString("D");
            //服务器的文件夹路径
            string path = Server.MapPath("~/UpLoadFiles");
            //获取完整的文件名称
            String fileName = this.up_file.FileName;
            //获取后缀
            String suffixName = fileName.Substring(fileName.LastIndexOf(".") + 1);

            try
            {
                Common.WriteFile(CommonCs.Exceptions.Operational, user.Uid, "上传文件[" + fileName + "]成功!");
                this.up_file.SaveAs(path + "\\" + guid + "-" + fileName);
                return true;
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Operational, user.Uid, "上传文件[" + fileName + "]失败!");
                return false;
            }
        }
    }
}